package com.gitee.wsl.platform.thread

import com.gitee.wsl.concurrent.executor.Executor
import kotlinx.atomicfu.locks.SynchronizedObject
import kotlinx.atomicfu.locks.synchronized
import kotlinx.coroutines.Runnable
import kotlin.native.concurrent.ObsoleteWorkersApi
import kotlin.native.concurrent.Worker

@OptIn(ObsoleteWorkersApi::class)
class NativeExecutor(
    private val worker: Worker
) : Executor {

    private val lock = SynchronizedObject()

    override var isDisposed: Boolean =false


    override fun exec(runnable: Runnable) {
        synchronized(lock) {
            worker.executeAfter(operation = runnable::run)
        }
    }

    override fun dispose() {
        synchronized(lock) {
            worker.requestTermination(processScheduledJobs = false)
            isDisposed = true
        }
    }
}
